<template>
  <el-dialog :title="!dataForm.roleId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-form-item label="角色名称" prop="role">
        <el-input v-model="dataForm.role" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item label="菜单权限">
        <el-cascader ref="menuRef" v-model="dataForm.menuIds" :options="menuOptions" :props="props" collapse-tags
          clearable placeholder="请选择" style="width: 100%;"></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getDataInfo, savaDataInfo } from '@/api/role';
  import { getDataList as getMenuList } from '@/api/menu';
  export default {
    data() {
      return {
        visible: false,
        dataForm: {},
        dataRule: {
          role: [{ required: true, message: '角色不能为空', trigger: 'blur' }],
        },
        // 
        props: { multiple: true, value: 'id', label: 'title', emitPath: false },
        menuOptions: [
          { id: "5", parentId: "0", path: "/receive", title: "接待管理" }
        ],
        indeterminateList: [], // 半选状态的菜单
      };
    },
    mounted() {
      getMenuList().then((res) => {
        if (res && res.code === 0) {
          this.menuOptions = this.filtersMenu(res.data);
        }
      }).catch((err) => {
        console.log(err);
      });
    },
    methods: {
      init(id) {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          this.dataForm = { roleId: id };
          if (this.dataForm.roleId) {
            getDataInfo(this.dataForm.roleId)
              .then((data) => {
                if (data && data.code === 0) {
                  this.dataForm = data.data;
                  this.dataForm.menuIds = this.dataForm.menuList.map(item => { return item.menuId });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 获取半选状态
            this.indeterminateList = [];
            const cascader = this.$refs.menuRef.getCheckedNodes();
            let checkNodes = cascader.map(item => { return item.value });
            cascader.forEach(node => { this.getIndeterminate(node) });
            let menuList = [...new Set([...checkNodes, ...this.indeterminateList])];
            let data = { ...this.dataForm, menuIds: menuList }
            savaDataInfo(data)
              .then((res) => {
                if (res && res.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                      this.$emit('refreshDataList');
                    },
                  });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
      // 获取半选状态的菜单
      getIndeterminate(node) {
        if (node.indeterminate) {
          this.indeterminateList.push(node.value);
        }
        if (node.parent) {
          this.getIndeterminate(node.parent)
        }
      },
      filtersMenu(arr) {
        return arr.filter(item => {
          if (item.children && item.children.length > 0) {
            this.filtersMenu(item.children);
          } else {
            delete (item.children);
          }
          return true;
        })
      }
    },
  };
</script>
